'use strict';

var config = require('../config/index.js');
var assert = require('assert');
var appUtils = require('../common/app_utils.js');
var testUtils = require('./test_utils.js');
var path = require('path');
var fs = require('fs');

describe('base64', function () {
  it('upload image as base64 string', function (done) {
    // remove file if exists
    var savePath = path.join(config.cwd, '/upload_tmp/from_base64.jpg');
    try {
      fs.unlinkSync(savePath);
    } catch (ex) {
      //console.log(savePath + ' not exists, just fine');
    }

    // TODO
    var fileUri = path.join(__dirname, './__mock.jpg');
    var buf = testUtils.readFileAsBuffer(fileUri);
    var base64Str = testUtils.bufToBase64(buf);
    var imgBase64Str = 'data:image/jpeg;base64,' + base64Str; // 这一项需要添加
    //console.log('base64Str:' + imgBase64Str.substr(0, 20));
    var theUrl = config.rootUrl + '/test/v1/base64test';
    testUtils.post_json(theUrl, {base64: imgBase64Str}, function (err, data) {
      //if (err)console.log('err');
      //if (data)console.log('data');
      //
      assert.equal(!!err, false);
      var str2 = testUtils.bufToBase64(testUtils.readFileAsBuffer(savePath));
      // str2 is saved file base64, base64Str is upload file base64
      assert.equal(str2, base64Str);

      done();
    });
  });
});